package com.h5lib.xlsx4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class XMLRelationReader {

    private XmlPullParser inputStream;

    public XMLRelationReader(XmlPullParser newPullParser) {
        inputStream = newPullParser;
    }

    public Properties next() throws XmlPullParserException, IOException {
    	int event;
        while( (event = inputStream.next()) != XmlPullParser.END_DOCUMENT ) {

            if( event == XmlPullParser.START_TAG ) {

                if( "Relationship".equals(inputStream.getName()) ) {

                    Properties properties = new Properties();

                    for(int i = 0; i < inputStream.getAttributeCount(); i++ ) {
                        String name = inputStream.getAttributeName(i).toLowerCase();
                        String value = inputStream.getAttributeValue(i);
                        properties.setProperty(name,value);
                    }
                    return properties;
                }
            }
        }

        return null;
    }

    public Map<String,String> all() throws XmlPullParserException, IOException {

        Map<String,String> relationship = new HashMap<String, String>();

        Properties properties;
        while ((properties = next()) != null) {
            relationship.put(properties.getProperty("id"), properties.getProperty("target"));
        }

        return relationship;
    }
}
